<?php

namespace app\admin\controller;

/**
 * 信箱中心
 */
class Mailbox extends base
{

    //前置方法
    protected $beforeActionList = [
        'map' => ['only' => 'index'],
    ];

    /**
     * 搜索
     */
    protected function map()
    {
        if ($this->request->isGet()) {
            $data = $this->request->param();
            $this->datas = array();
            if (!empty($data['start_time']) && !empty($data['end_time']) && (strtotime($data['start_time']) < strtotime($data['end_time']))) {
                $this->datas ['create_time'] = [
                    ['>', strtotime($data['start_time'])],
                    ['<', strtotime($data['end_time'])]
                ];
            }
            if (isset($data['status'])) {
                $this->datas ['status'] = $data['status'];
            }
            if (!empty($data['title'])) {
                $this->datas ['title'] = ['like', '%' . $data['title'] . '%'];
            }
            $this->datas ['type'] = $data['type'];
            $this->datas['lang'] = cookie('think_var_admin');
        }
    }

    public function view()
    {
        $one =array();
        $one['one'] = $this->model->where('type',1)->count();
        $one['two'] = $this->model->where('type',2)->count();
        $one['tree'] = $this->model->where('type',3)->count();
        $one['fore'] = $this->model->where('type',4)->count();
        $this->assign('one',$one);
        return $this->fetch();
    }


    /**
     * 显示用户留言内容
     * @param $id
     */
    public function read($id)
    {
        $data = $this->request->param();
        if ($this->request->isPost()) {
            $data['fid'] = $data['id'];
            $data['create_time'] = time();
            $data['adminname'] = $this->getLoginUser()->name;
            unset($data['id']);
            db('mailboxmass')->insert($data);
            $this->model->update(['status' => 2], ['id' => $id]);
            return ajax_info('留言成功！');
        }
        $data = $this->model->where('id', $id)->find();
        $list = db('mailboxmass')->where('fid', $id)->select();
        $this->model->update(['status' => 1], ['id' => $id]);

        $this->assign(
            [
                'list' => $list,
                'data' => $data
            ]
        );
        return $this->fetch();
    }

    /**
     * 发送消息
     */
    public function save()
    {

        $this->wechat = (new WeChat);
        $this->wechat->valid();
        //指定用户
        $d = $this->wechat->instance('Customservice')->sendTest($this->request->post('openid'), $this->request->post('center'));

        if ($d=='1') {
            $data = array(
                'openid' => $this->request->post('openid'),
                'center' => $this->request->post('center'),
                'who' => '1',
            );
            model('wechatmessageread')->save($data);
            return ajax_info('回复成功', 'index');
        }

        return ajax_info($d['errmsg']);
    }

    /**
     * 全部操作
     */
    public function dolistsave()
    {
        $post = $this->request->post();
        if (empty($post['id'])) {
            return ajax_info('请选择项目！', 'index');
        }
        foreach ($post['id'] as $key) {
            switch ($post['inputtype']) {
                case 'del':
                    $this->model->where('openid', $key)->delete();
                    model('wechatmessageread')->where('openid', $key)->delete();
                    break;
            }
        }
        return ajax_info('操作成功！', 'index');

    }

}
